﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using log4net;
using System.Reflection;
using Symbonix.Library.Utility;

/// <summary>
/// Summary description for Global
/// </summary>

// Load the configuration from the 'WebApp.dll.log4net' file
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
namespace Web
{
    public class Global : System.Web.HttpApplication
    {
        public Global() { }

        void Application_Start(object sender, EventArgs e)
        {
            // Initiating logging
            log4net.Config.XmlConfigurator.Configure();

            /***********************************************
             * This code block shutdowns file monitoring to 
             * escape session out while deleting folder
             * ********************************************/
            PropertyInfo p = typeof(System.Web.HttpRuntime).GetProperty("FileChangesMonitor", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
            object o = p.GetValue(null, null);
            FieldInfo f = o.GetType().GetField("_dirMonSubdirs", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
            object monitor = f.GetValue(o);
            MethodInfo m = monitor.GetType().GetMethod("StopMonitoring", BindingFlags.Instance | BindingFlags.NonPublic);
            m.Invoke(monitor, new object[] { });
            /*********************************************/
        }

        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown

        }

        void Application_Error(object sender, EventArgs e)
        {
            MethodBase methodBase = MethodBase.GetCurrentMethod();
            LogHelper.WriteLog(LogType.UnhandledLog, methodBase.DeclaringType.Name, methodBase.Name, 
                Server.GetLastError(), "Unhandled Server Error:");
        }

        void Session_Start(object sender, EventArgs e)
        {
            
        }

        void Session_End(object sender, EventArgs e)
        {
            
        }
    }
}